package com.acmedcare.framework.exchange.center.core.agent;

import com.acmedcare.framework.exchange.center.core.PluginMetadata;
import com.acmedcare.framework.exchange.center.core.metadata.IDataAgent;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import lombok.extern.slf4j.Slf4j;
import xyz.vopen.cartier.classpathscanner.scanner.ScanResult;

@Slf4j
@SuppressWarnings("unused")
public class DefaultAgentResolver implements IAgentResolver {

  @Override
  public void resolve(ConcurrentHashMap<String, AgentMetadata> agentMap, PluginMetadata metadata) {

    ClassLoader classLoader = metadata.getClassLoader();
    ScanResult result = metadata.getScanResult();
    List<String> agents = result.getNamesOfClassesImplementing(IDataAgent.class);
    if (agents.isEmpty()) {

      return;
    }

    agents.forEach(a -> {
      try {

        Class<?> agentClass = classLoader.loadClass(a);
        AgentMetadata agentMetadata = new AgentMetadata();
        agentMetadata.setAgentClass(agentClass);
        //agentMetadata.setInstance((IDataAgent) agentClass.newInstance());
        agentMap.put(agentClass.getName().toLowerCase(), agentMetadata);
      } catch (Exception e) {

        log.error("解析采集器异常,无法找到class文件", e);
      }
    });
  }
}
